package com.yatube.yygh.hospital.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yatube.yygh.hospital.mapper.HospitalMapper;
import com.yatube.yygh.hospital.repository.HospitalRepository;
import com.yatube.yygh.hospital.service.HospitalService;
import com.yatube.yygh.model.hosp.Hospital;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author Created with IDEA
 * @description --
 * @date 2024-04-23 23:04
 */
@Service
public class HospitalServiceImpl extends ServiceImpl<HospitalMapper, Hospital> implements HospitalService {

    @Resource
    private HospitalRepository hospitalRepository;

    @Override
    public void save(Map<String, Object> paramMap) {
        //
        String mapStr = JSONObject.toJSONString(paramMap);
        Hospital hospital = JSONObject.parseObject(mapStr, Hospital.class);
        //
        String hoscode = hospital.getHoscode();
        Hospital existHospital = hospitalRepository.findHospitalByHoscode(hoscode);
        // 存在修改
        if (null != existHospital){
            hospital.setStatus(existHospital.getStatus());
            hospital.setCreateTime(existHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else { // 不存在添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }
    }
}
